package com.ctgu.tengyun.controller.sparepart;

import com.ctgu.tengyun.bean.convert.FileConvert;
import com.ctgu.tengyun.bean.convert.SparePartConvert;
import com.ctgu.tengyun.bean.errorcode.CommonErrorCode;
import com.ctgu.tengyun.bean.request.SparePartDetail;
import com.ctgu.tengyun.bean.response.SparePartListResponse;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.SparePartInfo;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.service.sparepart.SparePartInfoService;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/26.
 */
@RestController
@RequestMapping(value = "/v1/sparepart/")
public class SparePartInfoController {

    @Resource
    private SparePartInfoService sparePartInfoService;

    @Resource
    private ProductInfoService productInfoService;

    @PostMapping(value = "save_or_update")
    public Object saveOrUpdate(@RequestBody SparePartInfo request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "productNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getPartNo()), "partNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getName()), "part name is required.");
        Validator.assertHasFields(null != request.getStockNum(), "part num is required.");
        Validator.assertHasFields(null != request.getSupplyPeriod(), "supply period is required.");
        FileConvert.splitFileBeanUrlPath(request.getGraphFile());

        String productNo = request.getProductNo();
        ProductInfo productInfo = productInfoService.selectByProductNo(productNo);
        Validator.assertResourceFound(null != productInfo, "product not found, productNo:" + productNo);

        if (null != request.getId()) {
            SparePartInfo partInfo = sparePartInfoService.selectByPrimaryKey(request.getId());
            Validator.assertResourceFound(null != partInfo, "sparepart not found, part id:" + request.getId());
            request.setVersion(partInfo.getVersion());

            sparePartInfoService.updateByPrimaryKeySelective(request);
        } else {

            try {
                sparePartInfoService.insertSelective(request);
            } catch (DuplicateKeyException e) {
                throw new ErrorCodeException(CommonErrorCode.duplicate_add, CommonErrorCode.duplicate_add.getMsg() + request.getPartNo());
            }
        }

        return ImmutableMap.of("id", request.getId());
    }

    @RequestMapping(value = "list")
    public SparePartListResponse list(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String productNo = ParameterTool.getParameterString(request, "productNo", "");
        String partNo = ParameterTool.getParameterString(request, "partNo", "");

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        Integer totalCount = sparePartInfoService.countSparePart(productNo, partNo);
        List<SparePartInfo> sparePartInfoList = Collections.emptyList();
        if (totalCount > 0) {
            sparePartInfoList = sparePartInfoService.selectSparePartList(productNo, partNo, pageNo, pageSize);
        }

        return SparePartConvert.toSparePartListResponse(totalCount, pageSize, sparePartInfoList);
    }

    @GetMapping(value = "detail/{id}")
    public SparePartDetail detail(@PathVariable("id") Long sparePartId) {
        SparePartInfo sparePartInfo = sparePartInfoService.selectByPrimaryKey(sparePartId);
        SparePartDetail detail = new SparePartDetail();
        BeanUtils.copyProperties(sparePartInfo, detail);

        return detail;
    }
}
